package online.westonline.com.westonline.adapter.notify;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.easy.west.westclient.api.constacts.bean.PersonBean;
import com.easy.west.westclient.api.login.bean.User;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;
import de.hdodenhof.circleimageview.CircleImageView;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.ui.OneContactActivity;

/**
 * 申请人/抄送人/执行人适配器
 * Created by xiatao on 2018/8/24.
 */

public class NotifyConstactsAdpter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PersonBean> userList = new ArrayList<>();

    public void setUserList(List<PersonBean> userList) {
        this.userList = userList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_list_item, parent, false);
        return new NotifyConstactsViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof NotifyConstactsViewHolder)
        {
            final NotifyConstactsViewHolder viewHolder = (NotifyConstactsViewHolder) holder;
            final PersonBean user = userList.get(position);
            viewHolder.tvTaskConstacts.setText(user.getName());
            //设置图片
            JMessageClient.getUserInfo(user.getAccess_token(), new GetUserInfoCallback() {
                @Override
                public void gotResult(int i, String s, UserInfo userInfo) {
                    if (i==0)
                    {
                        userInfo.getBigAvatarBitmap(new GetAvatarBitmapCallback() {
                            @Override
                            public void gotResult(int i, String s, Bitmap bitmap) {
                                viewHolder.imgTaskConstacts.setImageBitmap(bitmap);
                            }
                        });
                    }
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //需要转换成personbean
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("data",user);
                    Intent intent = new Intent(holder.itemView.getContext(), OneContactActivity.class);
                    intent.putExtras(bundle);
                    holder.itemView.getContext().startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return userList == null ? 0 : userList.size();
    }

    /**
     * viewholder
     */
    class NotifyConstactsViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_task_constacts)
        CircleImageView imgTaskConstacts;
        @BindView(R.id.tv_task_constacts)
        TextView tvTaskConstacts;
        public NotifyConstactsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);

        }
    }
}
